10 long long get_gcd(long long a
, long long b
, long long &x
, long long &y
){
11 if (b
> a
) return get_gcd(b
, a
, y
, x
);
16 long long g
, sub_x
, sub_y
;
17 g
= get_gcd(b
, a
%b
, sub_x
, sub_y
);
18 x
= sub_y
, y
= sub_x
- sub_y
*(a
/b
);
23 long long n
, n1
, n2
, c1
, c2
;
24 while (scanf("%lld", &n
)==1 && n
){
25 scanf("%lld %lld %lld %lld", &c1
, &n1
, &c2
, &n2
);
28 gcd
= get_gcd(n1
, n2
, a
, b
);
33 a
*= (n
/gcd
), b
*= (n
/gcd
);
34 assert(a
*n1
+ b
*n2
== n
);
37 long long lcm
= n1
/gcd
*n2
;
38 long long inc_a
= lcm
/n1
, inc_b
= lcm
/n2
;
41 long long times
= -a
/inc_a
;
42 if (-a
% inc_a
) ++times
;
43 a
+= times
*inc_a
, b
-= times
*inc_b
;
47 long long times
= -b
/inc_b
;
48 if (-b
% inc_b
) ++times
;
49 b
+= times
*inc_b
, a
-= times
*inc_a
;
54 if (a
< 0 || b
< 0) printf("failed\n");
57 if (a
> b
&& b
>= inc_b
){
58 long long times
= b
/inc_b
;
59 a
+= times
*inc_a
, b
-= times
*inc_b
;
62 else if (b
> a
&& a
>= inc_a
){
63 long long times
= a
/inc_a
;
64 a
-= times
*inc_a
, b
+= times
*inc_b
;
68 pair
<long long, long long> ans
;
69 long long best
= LLONG_MAX
;
71 //Listo, tenemos una posible soluciĆ³n.
72 if (a
*c1
+ b
*c2
< best
){
74 ans
= make_pair(a
, b
);
77 //Ahora bajemos esto al otro extremo
80 long long times
= a
/inc_a
;
81 a
-= times
*inc_a
, b
+= times
*inc_b
;
83 if (a
*c1
+ b
*c2
< best
){
85 ans
= make_pair(a
, b
);
90 long long times
= b
/inc_b
;
91 a
+= times
*inc_a
, b
-= times
*inc_b
;
92 if (a
*c1
+ b
*c2
< best
){
94 ans
= make_pair(a
, b
);
99 printf("%lld %lld\n", ans
.first
, ans
.second
);